﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using Newtonsoft.Json;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for Category
    /// </summary>
    public class Category : BaseHandler
    {
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        
        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        private string getCategoryTable(string categoryType)
        {
            string tableName = string.Empty;

            if (categoryType == "SMS")
                tableName = " crm_smstemplatecategory ";
            else if (categoryType == "Dual")
                tableName = " crm_dualtemplatecategory";
            else if (categoryType == "EMail")
                tableName = " crm_emailtemplatecategory";

            return tableName;
        }
        public void SelectParentCategory(int category_id, string CategoryType)
        {
            string tableName=string.Empty;
            try
            {
                tableName = getCategoryTable(CategoryType);
                string strJson = CategoryNodeHandler.getParentcategory(category_id,tableName);
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void DisplayCategoryList(string CategoryType)
        {
            bool isSMS = false;
            bool isDual = false;
            try
            {
                if (CategoryType == "SMS")
                {
                    isSMS = true;
                    isDual = false;
                }
                else if(CategoryType=="EMail")
                {
                    isSMS = false;
                    isDual = false;
                }
                else if (CategoryType == "Dual")
                {
                    isSMS = false;
                    isDual = true;
                }
                
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetCategoryNodesList(isSMS,true,isDual));
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void saveCategory(string CategoryType,int CategoryID,string CategoryName,int ParentId)
        {
            string tableName = string.Empty;
             
            try
            {
                tableName = getCategoryTable(CategoryType);
                int response = CategoryNodeHandler.saveCategory(tableName, CategoryID, CategoryName, ParentId);
                context.Response.Write(response.ToString());
            }
            catch (Exception ex)
            {
               throw ex;
            }

        }

        public void deleteCategory(string CategoryType, int CategoryID)
        {
            string tableName = string.Empty;
            try
            {
                tableName = getCategoryTable(CategoryType);
                int response=CategoryNodeHandler.deleteCategory(CategoryID, tableName);
                context.Response.Write(response.ToString());
            }
            catch (Exception ex)
            {
               throw ex;
            }

        }
    }
  
}